package com.regent.central.infrastructure.service.bean;

import com.regent.central.infrastructure.service.DomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

/**
 * Created by kk 胡振立 on 2018/3/12.
 */
public class DomainServiceBean<M extends Mapper<T>, T> implements DomainService<T> {

    @Autowired
    protected M mapper;

    @Transactional(readOnly = true)
    public T selectOne(T entity) {
        return mapper.selectOne(entity);
    }

    @Transactional(readOnly = true)
    public T selectById(Object id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Transactional(readOnly = true)
    public List<T> selectList(T entity) {
        return mapper.select(entity);
    }

    @Transactional(readOnly = true)
    public List<T> selectListAll() {
        return mapper.selectAll();
    }

    @Transactional(readOnly = true)
    public Long selectCount(T entity) {
        return (long) mapper.selectCount(entity);
    }

    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    public int insert(T entity) {
        return mapper.insert(entity);
    }

    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    public int insertSelective(T entity) {
        return mapper.insertSelective(entity);
    }

    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    public int delete(T entity) {
        return mapper.delete(entity);
    }

    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    public int deleteById(Object id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    public int deleteByExample(Object example) {
        return mapper.deleteByExample(example);
    }

    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    public int updateById(T entity) {
        return mapper.updateByPrimaryKey(entity);
    }

    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    public int updateSelectiveById(T entity) {
        return mapper.updateByPrimaryKeySelective(entity);
    }

    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    public int updateByPrimaryKeySelective(T entity) {
        return mapper.updateByPrimaryKeySelective(entity);
    }

    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    public int updateByExampleSelective(T entity, Object example) {
        return mapper.updateByExampleSelective(entity, example);
    }

    @Transactional(readOnly = true)
    public List<T> selectByExample(Object example) {
        return mapper.selectByExample(example);
    }

    @Transactional(readOnly = true)
    public int selectCountByExample(Object example) {
        return mapper.selectCountByExample(example);
    }

    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    @Override
    public <T> T doAtomically(Atom<T> atom) {
        if (atom == null) throw new NullPointerException("atom == null");
        else return atom.execute();
    }
}
